package com.gupao.config;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.provider.ClientDetails;

import com.gupao.entity.OauthClientDetails;
import com.gupao.util.CommonUtils;

public class ThirdLoginClientDetails implements ClientDetails {
	
	private OauthClientDetails  oauthClientDetails;
	
	private Set<String> scope = Collections.emptySet();
	
	public ThirdLoginClientDetails(OauthClientDetails oauthClientDetails) {
		super();
		this.oauthClientDetails = oauthClientDetails;
	}

	public String getClientId() {
		return oauthClientDetails.getClientId();
	}

	public Set<String> getResourceIds() {
		// TODO Auto-generated method stub
		return CommonUtils.transformStringToSet(oauthClientDetails.getResourceIds());
	}

	public boolean isSecretRequired() {
		return oauthClientDetails.getClientSecret() != null;
	}

	public String getClientSecret() {
		// TODO Auto-generated method stub
		return oauthClientDetails.getClientSecret();
	}

	public boolean isScoped() {
		return this.scope != null && !this.scope.isEmpty();
	}

	public Set<String> getScope() {
		// TODO Auto-generated method stub
		this.scope=CommonUtils.transformStringToSet(oauthClientDetails.getScope());
		return CommonUtils.transformStringToSet(oauthClientDetails.getScope());
	}

	public Set<String> getAuthorizedGrantTypes() {
		// TODO Auto-generated method stub
		return CommonUtils.transformStringToSet(oauthClientDetails.getAuthorizedGrantTypes());
	}

	public Set<String> getRegisteredRedirectUri() {
		// TODO Auto-generated method stub
		return CommonUtils.transformStringToSet(oauthClientDetails.getWebServerRedirectUri());
	}

	public Collection<GrantedAuthority> getAuthorities() {
		// TODO Auto-generated method stub
		return (oauthClientDetails.getAuthorities()!=null&&oauthClientDetails.getAuthorities().trim().length()>0)?
                AuthorityUtils.commaSeparatedStringToAuthorityList(oauthClientDetails.getAuthorities()):null;
	}

	public Integer getAccessTokenValiditySeconds() {
		return oauthClientDetails.getAccessTokenValidity();
	}

	public Integer getRefreshTokenValiditySeconds() {
		// TODO Auto-generated method stub
		return oauthClientDetails.getRefreshTokenValidity();
	}

	public boolean isAutoApprove(String scope) {
		if("true".equals(oauthClientDetails.getAutoapprove())) {
			return true;
		}else {
			return false;
		}
	}

	public Map<String, Object> getAdditionalInformation() {
		// TODO Auto-generated method stub
		return null;
	}

	

}
